package com.class05.json2;

import com.class05.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.DispatcherServlet;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @ClassDescription:
 * @JdkVersion: 17
 * @Author: yxy
 * @Created: 2023/12/7 16:27
 */
//@Controller
@RequestMapping("json")
//@ResponseBody////返回json的注解，添加到类和方法上
@RestController //==@Controller+ @ResponseBody
public class JsonController {
    /**
     * @ResponseBody 数据直接放入响应体返回！ 也不会走视图解析器
     *      加了@ResponseBody后快速查找视图，转发和重定向都不生效了！
     * @return
     */
    @GetMapping("data")
    public User data(){
        //对象-》 json->{}
        //集合-》 json->[]
        return new User("zhangsan",18);
        //user -> handlerAdapter -> json ->
//        @ResponseBody  -> json直接返回 （前后端分离模式）
    }



    @GetMapping("data1")
    public List<User> data1(){

        List<User> userList = new ArrayList<>();
        userList.add(new User("zhangsan",18));
        userList.add(new User("list",20));
        userList.add(new User("wangwu",30));
        userList.add(new User("zhaoliu",25));
        userList.add(new User("qianqi",60));
//        取前三条
//        List<User> collect = userList.stream().limit(3).
//        collect(Collectors.toList());
       /* 降序
       List<User> collect = userList.stream()
                .sorted(Comparator.comparingInt(User::getAge).reversed())
                .collect(Collectors.toList());*/
        //升序
        List<User> collect = userList.stream()
                .sorted(Comparator.comparingInt(User::getAge))
                .collect(Collectors.toList());
        return collect;
    }
}
